package com.jason.apps.dashboard;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.actionbarsherlock.app.SherlockFragment; 
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.apps.essentials.ConsumeWebService;
import com.apps.realtyportal.R;

import android.R.color;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class ShowChart extends SherlockFragment {
	
    public static final String TYPE = "type";
    private GraphicalView mChartView;
    XYMultipleSeriesRenderer multiRenderer;
    XYMultipleSeriesDataset dataset;
    String getyeartext=null;
    int year=0;
    private LinearLayout showyear;
    LinearLayout layout;
    private Button go;
    private EditText yeartext;
    double[] frequency;
    SherlockFragmentActivity fa;
    ConsumeWebService consume=new ConsumeWebService();
    
    String[] month = new String[] {
            "Jan", "Feb" , "Mar", "Apr", "May", "Jun",
            "Jul", "Aug" , "Sep", "Oct", "Nov", "Dec"
        };
    
	HashMap<String, String> map;
	ArrayList<Double> mylist;

	
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	View v=inflater.inflate(R.layout.showchart, container,false);
	fa=(SherlockFragmentActivity)super.getActivity();

    Calendar cal=Calendar.getInstance();
	year=cal.get(Calendar.YEAR);
	multiRenderer = new XYMultipleSeriesRenderer();
    dataset = new XYMultipleSeriesDataset();
    
    showyear=(LinearLayout)v.findViewById(R.id.showLayout_graph);
    go=(Button)v.findViewById(R.id.show_go);
    yeartext=(EditText)v.findViewById(R.id.show_year);
    layout = (LinearLayout)v.findViewById(R.id.chart);
    
    new LoadGraph().execute();
    
    go.setOnClickListener(new OnClickListener() {
		

		public void onClick(View v) {
			// TODO Auto-generated method stub
			new LoadGraph().execute();
			showyear.setVisibility(View.GONE);
		}
	});


	return v;
}
public void Show(){
	showyear.setVisibility(View.VISIBLE);
}
@Override
public void onResume() {
    super.onResume();  
}
public class LoadGraph extends AsyncTask<Void, Void, Void> {

     ProgressDialog dialog;


 	    protected void onPreExecute() {
 	    	dialog = new ProgressDialog(fa);
 	            dialog.setMessage("Loading...");
 	            dialog.show();
 	    }
	    protected Void doInBackground(final Void... unused) {
	    	
	    	List<Double> freq=new ArrayList<Double>();
	    	
	    	ConsumeWebService c=new ConsumeWebService();
	    	Calendar cal=Calendar.getInstance();
	    	getyeartext=yeartext.getText().toString().trim();
	    	
	    	if(getyeartext.equals("")){
	    		year=cal.get(Calendar.YEAR);
	    		Log.e("year from calendat", ""+year);
	    	}
	    	else{
	    		year=Integer.parseInt(getyeartext);
	    		Log.e("year from editext", ""+year);
	    	}
	    	String data=c.getGraphdata(year);
	    	try{
	    		
	    		JSONArray array = new JSONArray(data);
				
	    		for (int i = 0; i < array.length(); i++) {
					JSONObject e = array.getJSONObject(i);
					freq.add(e.getDouble("frequency"));
				}
	    		
	    	}catch(Exception e){
	    		Log.e("", e.getMessage());
	    	}
	        XYSeries incomeSeries = new XYSeries("Sold");
	        
	        for(int i=0;i<month.length;i++){
	            incomeSeries.add(i, freq.get(i));;
	        }
	        freq.clear();

	        dataset.addSeries(incomeSeries);

	        XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
	        incomeRenderer.setColor(Color.argb(100, 132, 185, 201));
	        incomeRenderer.setPointStyle(PointStyle.CIRCLE);
	        incomeRenderer.setFillBelowLine(true);
	        incomeRenderer.setFillBelowLineColor(Color.argb(100, 187, 225, 248));
	        incomeRenderer.setFillPoints(true);
	        incomeRenderer.setLineWidth(6);
	        
	        incomeRenderer.setDisplayChartValues(true); 
	        multiRenderer.setXLabels(0);
	        multiRenderer.setChartTitle("Sold Property");
	        multiRenderer.setXTitle("Year");
	        multiRenderer.setYTitle("Frequency");
	        multiRenderer.setApplyBackgroundColor(true);
	        multiRenderer.setBackgroundColor(Color.WHITE);
	        multiRenderer.setZoomButtonsVisible(false);
	        multiRenderer.setPointSize(8);
	        multiRenderer.setShowLegend(false);
	        
	        for(int i=0;i<month.length;i++){
	            multiRenderer.addXTextLabel(i, month[i]);
	        }
	        multiRenderer.addSeriesRenderer(incomeRenderer);
	       
			
	   
	        return null; 
	    }
	    protected void onPostExecute(Void result) {	
	    	if (mChartView == null) {
	            
	            mChartView = ChartFactory.getLineChartView(getActivity(), dataset,
	                    multiRenderer);
	            multiRenderer.setSelectableBuffer(100);
	            layout.addView(mChartView, new LayoutParams(
	                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
	        } else
	        	
	        	mChartView.refreshDrawableState();
	            mChartView.repaint();
	            
	            	    		
			if (dialog!=null) {
	            dialog.dismiss();
	            }  
	     }
	  }	
}